<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }
  </style>
</head>
<body>
  <script src="./node_modules/three/build/three.js"></script>
  <script>

    const vw = window.innerWidth;
    const vh = window.innerHeight;

    // 创建场景
    const scene = new THREE.Scene();
    scene.background = new THREE.Color('black');
    // 创建相机（上帝视角）
    const camera = new THREE.PerspectiveCamera( 75, vw / vh, 0.1, 1000 );
    camera.position.x = 1;
    camera.position.y = 1;
    camera.position.z = 3;
    console.log(camera);
    // 渲染器
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize( vw, vh );
    document.body.appendChild( renderer.domElement );

    // 几何描述 （半径，水平平滑度，垂直平滑度）
    const geometry = new THREE.BoxGeometry(1, .5, .7);
    // 材质
    const material = new THREE.MeshPhysicalMaterial( { color: 0x333333 } );
    // 生成一个立方体(-2, 0, 0)
    const cube = new THREE.Mesh( geometry, material );
    scene.add( cube );

    // 几何描述 （半径，水平平滑度，垂直平滑度）
    const geometry1 = new THREE.SphereGeometry(.1, 32, 32);
    // 材质
    const material1 = new THREE.MeshPhysicalMaterial( { color: 0xffffff } );
    // 生成一个立方体(-2, 0, 0)
    const ball = new THREE.Mesh( geometry1, material1 );
    scene.add( ball );

    // // 几何描述 （半径，水平平滑度，垂直平滑度）
    // const geometry1 = new THREE.SphereGeometry();
    // // 材质
    // const material1 = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
    // // 生成一个立方体(2, 0, 0)
    // const cube1 = new THREE.Mesh( geometry, material );
    // cube.position.x = 2;
    // scene.add( cube1 );

    // 光照
    const light1 = new THREE.SpotLight( new THREE.Color('pink'), 3 );
    // light1.position.set( 0, 10, 5 );
    scene.add( light1 );
    // 光照
    const light2 = new THREE.SpotLight( 0xaaaaaa, 1.5 );
    light2.position.set( -5, -10, 0 );
    scene.add( light2 );

    let alpha = 0;
    const radius = 5;
    let y = 1.5;
    let direction = 'up';
    const v = 5;

    function animate() {
      requestAnimationFrame( animate );

      // cube.rotation.x += 0.01;
      // cube.rotation.y += 0.01;
      const x = radius * Math.cos(alpha);

      // [1, 5]
      if (direction === 'up') {
        y += 4 * v / (Math.PI * 200);
        if (y > 5) {
          direction = 'down';
        }
      } else {
        y -= 4 * v / (Math.PI * 200);
        if (y < 1) {
          direction = 'up';
        }
      }

      const z = radius * Math.sin(alpha);

      light1.position.set(x, y, z);
      camera.position.set(x, y, z);
      camera.lookAt(0, 0, 0);
      ball.position.set(x, y, z);

      alpha += 0.01 * v; // 628帧

      renderer.render( scene, camera );
    };

    animate();
  </script>
</body>
</html>